﻿using System;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.ComponentModel.Design;
using Microsoft.Win32;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using System.Collections.Generic;
using VsxFactory.Modeling.VisualStudio.Synchronization;
using VsxFactory.Modeling.VisualStudio;
using VsxFactory.Modeling.Strategies;
using VsxFactory.Modeling.Strategies.VisualStudio;
using VsxFactory.Modeling.StrategyManager.VisualStudio;

namespace VsxFactory.Modeling.StrategyManager
{
    [PackageRegistration(UseManagedResourcesOnly = true)]
    [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
    [Guid(GuidList.guidStrategyManagerPackagePkgString)]
    [ProvideService(typeof(IStrategyManager))]
    [ProvideMenuResource("Menus.ctmenu", 4)]
    [ProvideAutoLoad(VSConstants.UICONTEXT.SolutionExists_string)]
    [ProvideOptionPage(typeof(ToolsOptionsPage), "Environment", "Strategy Manager", 101, 102, true)]
    [ProvideProfile(typeof(ToolsOptionsPage), "Environment", "Strategy Manager", 101, 102, true)]
    public class StrategyManagerPackage : Package
    {
        private StrategyManager strategyManager;

        public StrategyManagerPackage()
        {
            Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}", this.ToString()));
        }
        
        protected override void Initialize()
        {
            Trace.WriteLine (string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString()));
            base.Initialize();

            try
            {
                strategyManager = new StrategyManager(this);
                strategyManager.InitializeServices(this, this);

                ((IServiceContainer)this).AddService(typeof(SStrategyManager), strategyManager, true);

                // Add our command handlers for menu (commands must exist in the .vsct file)
                OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
                if (null != mcs)
                {
                    CommandID commandId;
                    OleMenuCommand menuItem;

                    // Affichage de la fenetre d'administration des strategies
                    commandId = new CommandID(GuidList.guidStrategyManagerPackageCmdSet, (int)PkgCmdIDList.ShowStrategyManagerUI);
                    menuItem = new OleMenuCommand( strategyManager.ExecuteShowUI, null, strategyManager.CanExecuteShowUI, commandId);
                    mcs.AddCommand(menuItem);

                    // Generation
                    commandId = new CommandID(GuidList.guidStrategyManagerPackageCmdSet, (int)PkgCmdIDList.GenerateAll);
                    menuItem = new OleMenuCommand(strategyManager.ExecuteGenerate, strategyManager.ChangeGenerate, strategyManager.CanExecuteGenerate, commandId);
                    mcs.AddCommand(menuItem);
                }
            }
            catch
            {
                Debug.Fail("Initialization strategies package error"); // TODO
            }
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            if (disposing)
            {
                if( strategyManager != null)
                    strategyManager.Dispose();
            }
        }
    }
}
